-- search for the fourth word in str, starting at the cursor position
-- 0 and continuing until the end (str.size)
global args := #(str, 4, 0, str.size) as Quad
findNthContext args @word
-- print out the word returned
copyFromTo args[1] args[3] args[4]
-- search for the second set of characters bounded by ":"
global s := "first name:last name:street address:city:state"
global args := #(s, 2, 0, s.size) as Quad
findNthContext args (r -> r == ":"[1])
-- print out the characters returned
copyFromTo args[1] args[3] args[4]
-->>>
--<<<
-- Kaleida Labs, Inc.
-- Field Guide to the ScriptX Language
-- chapter 7, example 2
-- create a string
global myString := "Newton devised a new system."
-- create a StringIndex object
global strIndex := new StringIndex string:myString
-- create a SearchContext object suitable for beginning the search at
-- the beginning of the string
global sc := initialSearchContext strIndex 0
-- search for the first occurrence of "new" as a whole word
sc := searchIndex strIndex "new" sc true
-- print out the result
copyFromTo sc.string sc.startOffset sc.endOffset
-- search for the first occurrence of "new" as a whole word or as part
-- of a larger word. We need a new "sc" to start at the beginning of the string.
sc := initialSearchContext strIndex 0
searchIndex strIndex "new" sc false
-- print out the result (Note that searchIndex is not case sensitive.)
copyFromTo sc.string sc.startOffset sc.endOffset
-->>>
-- create a string
global str := “This is a sample string.”
-- search for the fourth word in str, starting at the cursor position -- 0 and continuing until the end (str.size)
global args := #(str, 4, 0, str.size) as Quad
findNthContext args @word
-- print out the word returned
copyFromTo args[1] args[3] args[4]
-- search for the second set of characters bounded by ":"
s := "first name:last name:street address:city:state"
args := #(s, 2, 0, size.s) as Quad
findNthContext args (r -> r == ":"[1])
-- print out the characters returned
copyFromTo args[1] args[3] args[4]
A second global function for searching strings is searchIndex, which finds the first occurrence of the string you want to match. (Note that the string you want to match must be at least three characters long and cannot contain any spaces.) Searching is fast because searchIndex is actually searaching a signature index, which is built automatically when you create a StringIndex object and supply a string for its string instance variable.
searchIndex strIndex match searchContext wholeWord
This function searches the StringIndex object strIndex for the first occurrence of match using searchContext to tell it where to search. The last argument, wholeWord, is either true or false, indicating whether the match must be a whole word (as opposed to only part of a word).
The following code example demonstrates using searchIndex. It will be easier to follow if you have read the entries for SearchContext and StringIndex in ScriptX Class Reference.
-- create a string
global myString := "Newton devised a new system."
-- create a StringIndex object
global strIndex := new StringIndex string:myString
-- create a SearchContext object suitable for beginning the search at -- the beginning of the string
global sc := initialSearchContext strIndex 1
-- search for the first occurrence of "new" as a whole word
searchIndex strIndex "new" sc true
-- print out the result
copyFromTo sc.string sc.startOffset sc.endOffset
"new"
-- search for the first occurrence of "new" as a whole word or as part -- of a larger word
searchIndex strIndex "new" sc false
-- print out the result (Note that searchIndex is not case sensitive.)
copyFromTo sc.string sc.startOffset sc.endOffset
"Newton"
global myString := "Able was I ere I saw Elba" as String
select first word where it[1] = "s"[1] in myString
select first word where (getFirst it) = "s"[1] in myString
select first word where (findRange it "s") = 1 in myString
global str := "hello there"
contexts str
str := undefined
global myList := new LinkedList
contexts myList
-- Selecting Named Objects
class MyClass () end -- a parent class you want to add a name to
class NamedClass (MyClass)
instance vars name
end
-- a heterogeneous collection of objects, all inheriting from NamedClass